var city = localStorage.getItem("city");
var scenics = localStorage.getItem("scenics");
scenics = JSON.parse(scenics);
if (city == "") city = "深圳";

//读取本地保存的推荐景点信息，并展示出来
function show_scenics() {
	console.log(scenics);
	if (scenics.code == 1) {
		$(".slider").empty();
		$.each(scenics.content, function(index, item) {
			var index = index + 1;
			if (item.pictureUrl != null) {
				if (item.pictureUrl.indexOf("http:") < 0) $(".slider").append("<li class='slider-item slider-item"+ index +"' style='background-image: url(/youu-restapi/" + item.pictureUrl + ");background-repeat: round;'></li>");
				else $(".slider").append("<li class='slider-item slider-item"+ index +"' style='background-image: url(" + item.pictureUrl + ");background-repeat: round;'></li>");
			}
		});
	}
};

// 根据城市获取图片
function get_pic() {
	$("#navbar #location_div").remove();
	if (city == null)
		return false;
	$("#navbar")
			.append(
					"<br/><div id='location_div' style='color: white;'><img alt='' src='assets/img/dingwei.png' style='heigth:20px; width: 20px; float: right'> "
							+ city + "</div>");
	$.get("/youu-restapi/show/scenics_details/recommend", "city=" + city,
			function(data) {
				if (data.code == 1) {
					localStorage.setItem("scenics", JSON.stringify(data));
					scenics = data;
					show_scenics();
				}
			}, "json");
}

// 百度定位保存位置
function save_city() {
	var geolocation = new BMap.Geolocation();
	geolocation.getCurrentPosition(function(r) {
		if (this.getStatus() == BMAP_STATUS_SUCCESS) {
			var mk = new BMap.Marker(r.point);
			// console.log(r);
			city = r.address.city;
			city = city.replace(/市/g, "");
			city = city.replace(/省/g, "");
			city = city.replace(/自治区/g, "");
			city = city.replace(/区/g, "");
			if (city == "") city = "深圳";
			localStorage.setItem("city", city); // 保存城市名
			localStorage.setItem("lng", r.point.lng); // 保存经度
			localStorage.setItem("lat", r.point.lat); // 保存纬度
			get_pic();
		} else {
			alert('failed' + this.getStatus());
		}
	}, {
		enableHighAccuracy : true
	});
}

// 判断本地是否有位置信息
if (city == null) {
	save_city();
}

show_scenics();
get_pic();
save_city();